var sliderWidth = 96;
const app = getApp();
Page({
  data: {
    tabs: ["商品优惠券", "运费优惠券"],
    activeIndex: 0,
    list:[],
    str:'',
    certification: false,
    userInfo:{},
    sliderOffset: 0,
    sliderLeft: 0
  },
  onLoad: function (options){
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(res) {
              var str = JSON.parse(res.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res) {
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data,
                          certification: true
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        } else {
          that.setData({
            certification: false
          })
          wx.showToast({
            title: '请点击收货地址获取授权信息',
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      }
    })
    if (options.type){
      wx.setNavigationBarTitle({
        title: '领券中心',
      })
      app.util.request({
        url: 'Index/Index/CouponsList',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          that.setData({
            list: res.data,
            str:'立即领取'
          })
        }
      })
    }else{
      wx.setNavigationBarTitle({
        title: '我的红包',
      })
      app.util.request({
        url: 'Index/Index/getCoupons',
        data: {
          openid: app.globalData.userInfo.openid
        },
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          that.setData({
            list: res.data.data,
            str: '去使用'
          })
        }
      })
    }
    wx.getSystemInfo({
      success: function (res){
        that.setData({
          sliderLeft: (res.windowWidth/that.data.tabs.length-sliderWidth) / 2,
          sliderOffset:res.windowWidth/that.data.tabs.length* that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  use:function(e){
    const that = this;
    const data = that.data.list;
    const index = e.currentTarget.dataset.index;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      coupons: data[index]
    });
    wx.navigateBack({
      delta: 1,
    })
    // wx.reLaunch({
    //   url: '../index/index'
    // })
  },
  bindGetUserInfo:function(e){
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(a) {
              var str = JSON.parse(a.rawData);
              wx.login({
                success(res) {
                  if (res.code) {
                    app.util.request({
                      url: 'Index/Index/getOpenid',
                      data: {
                        avatarUrl: str.avatarUrl,
                        city: str.city,
                        country: str.country,
                        gender: str.gender,
                        language: str.language,
                        nickName: str.nickName,
                        province: str.province,
                        code: res.code
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success(res){
                        app.globalData.userInfo = res.data;
                        that.setData({
                          userInfo: res.data
                        })
                        const index = e.currentTarget.dataset.index;
                        const data = that.data.list;
                        var openid = '';
                        if (JSON.stringify(app.globalData.userInfo) == '{}') {
                          if (JSON.stringify(that.data.userInfo) == '{}') {
                            console.log('授权失败');
                          } else {
                            openid = that.data.userInfo.openid;
                          }
                        } else {
                          openid = app.globalData.userInfo.openid;
                        }
                        app.util.request({
                          url: 'Index/Index/receiveCoupons',
                          data: {
                            openid: openid,
                            id: data[index]['id']
                          },
                          header: {
                            'content-type': 'application/json'
                          },
                          success(res) {
                            if (res.data.code === 1) {
                              wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                                duration: 2000
                              });
                              return false;
                            } else {
                              wx.showToast({
                                title: '领取成功',
                                icon: 'success',
                                duration: 2000
                              });
                              return false;
                            }
                          }
                        })
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  receive:function(e){
    const that = this;
    const index = e.currentTarget.dataset.index;
    const data = that.data.list;
    var openid = '';
    if (JSON.stringify(app.globalData.userInfo) == '{}') {
      if (JSON.stringify(that.data.userInfo) == '{}') {
        console.log('授权失败');
      } else {
        openid = that.data.userInfo.openid;
      }
    } else {
      openid = app.globalData.userInfo.openid;
    }
    app.util.request({
      url: 'Index/Index/receiveCoupons',
      data: {
        openid: openid,
        id: data[index]['id']
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if(res.data.code === 1){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          return false;
        }else{
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 2000
          });
          return false;
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})